﻿using PLCS.App.LogManager.Service;
using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.InterfaceApp.Dtos;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Client.IService.LogManager;
using PLCS.Client.Service.LogManager;

using Prism.Commands;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLCS.App.LogManager.ViewModels
{
    internal class OperationLogViewModel : NavigationPagedCurdViewModel<OperationLogDto, OperationLogEditDto, IOperationLogService>
    {
        private string _startTime = string.Empty, _endTime = string.Empty;
        private bool _isRightDrawerOpen;
        private ObservableCollection<PropertyNodeItem> _itemList;

        public bool IsRightDrawerOpen
        {
            get { return _isRightDrawerOpen; }
            set { _isRightDrawerOpen = value; RaisePropertyChanged(); }
        }

        public string StartTime
        {
            get { return _startTime; }
            set { _startTime = value; RaisePropertyChanged(); }
        }

        public string EndTime
        {
            get { return _endTime; }
            set { _endTime = value; RaisePropertyChanged(); }
        }

        private string _view;

        public string ViewName
        {
            get { return _view; }
            set { SetProperty(ref _view, value); }
        }

        private string _operationType;

        public string OperationType
        {
            get { return _operationType; }
            set { SetProperty(ref _operationType, value); }
        }

        public ObservableCollection<PropertyNodeItem> ItemList
        {
            get { return _itemList; }
            set { _itemList = value; RaisePropertyChanged(nameof(ItemList)); }
        }

        public DelegateCommand<string> OpenObjectCommand { get; set; }

        public OperationLogViewModel(IOperationLogService service) : base(service)
        {
            OpenObjectCommand = new DelegateCommand<string>(OpenObject);
        }

        private void OpenObject(string obj)
        {
            IsRightDrawerOpen = true;
            if (string.IsNullOrEmpty(obj))
            {
                ItemList = new ObservableCollection<PropertyNodeItem>();
            }
            try
            {
                IDataConvert dataConvert = DataConvertFactory.CreateInstance(Domain.Shared.Logs.InterfaceDataType.Json);
                ItemList = (dataConvert.GetTreeModels(obj)).ToObservable();
            }
            catch (Exception ex)
            {
                ItemList = new ObservableCollection<PropertyNodeItem>();
            }
        }

        public override async Task GetPageData(int pageIndex)
        {
            try
            {
                UpdateLoading(true);
                var res = await _service.GetListBySearchAsync((pageIndex - 1) * PageSize, PageSize, StartTime, EndTime, View, OperationType);
                TotalCount = (int)res.TotalCount;
                SetPageCount();
                DataGridList = res.Items.ToObservable();
            }
            finally
            {
                UpdateLoading(false);
            }
        }
    }
}